<template>
  <div>
    <div class="info" v-for="item in infoList" :key="item.id">
      <h1>{{ item.title }}</h1>
      <p>
        <span>发表时间: {{ item.add_time | dateFormat }}</span>
        <span>点击:{{ item.click }}次</span>
      </p>
      <hr>
      <div class="image">
        <img class="preview-img" v-for="(item, index) in list" :src="item.src" height="100" @click="$preview.open(index, list)" :key="item.src">
      </div>
      <div class="details" v-html="item.content"></div>
    </div>
    
    <comment class="comments" :id="id"></comment>
  </div>
</template>
<script>
import comment from "../subcomponents/comment.vue";

export default {
  data() {
    return {
      id: this.$route.params.id,
      infoList: {}, //详细信息
      list: [] //图片缩略图
    };
  },
  created() {
    this.getPhotoInfos();
    this.getPhotoPreview();
  },
  methods: {
    getPhotoInfos() {
      this.$http.get("api/getimageInfo/" + this.id).then(result => {
        if (result.body.status === 0) {
          this.infoList = result.body.message;
        }
      });
    },
    getPhotoPreview() {
      this.$http.get("api/getthumimages/" + this.id).then(result => {
        /* 缩略图的每一项都必须有宽和高，循环每一项，为每一项添加宽高 */
        if (result.body.status === 0) {
          result.body.message.forEach(item => {
            item.w = 600;
            item.h = 400;
          });
          this.list = result.body.message;
        }
      });
    }
  },
  components: {
    comment
  }
};
</script>
<style lang="scss" scoped>
div {
  padding: 0 5px;
  .info {
    h1 {
      font-size: 16px;
      text-align: center;
      padding: 10px 0;
      color: #309ef4;
    }
    p {
      display: flex;
      justify-content: space-between;
    }
    hr {
      margin: 8px 0;
      color: #d4d4d4;
    }
    .image {
      img {
        margin: 10px;
      }
    }
    .details {
      font-size: 14px;
      line-height: 2.1em;
    }
  }

  .comments {
    padding: 0;
    padding-top: 10px;
  }
}
</style>

